package com.lk.api.v1;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;


/**
 * 对外开放接口
 */
@Controller
@RequestMapping("${extapi}")
public class ExternalController {



    @RequestMapping("/re")
    @ResponseBody
    public String to(HttpServletRequest request){
        System.out.println("==================================================");
        try {
            ServletInputStream inputStream = request.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            StringBuffer sb = new StringBuffer("");
            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (br != null) {
                br.close();
            }
            JSONObject jsonObject = JSONObject.parseObject(sb.toString());
            String msgType = jsonObject.getString("msg_type");
            JSONObject content = jsonObject.getJSONObject("content");

            System.out.println(sb.toString());
            System.out.println(content.getString("text"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }



        return "aaa";
    }



}
